package com.yjt.admin.system.controller;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yjt.admin.system.entity.Post;
import com.yjt.admin.system.form.PostForm;
import com.yjt.admin.system.param.PostParam;
import com.yjt.admin.system.service.PostService;
import com.yjt.admin.system.vo.PostVO;
import com.yjt.common.enums.ModulEnum;
import com.yjt.common.enums.OperTypeEnum;
import com.yjt.common.log.annotation.OperLog;
import com.yjt.common.vo.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 岗位表 前端控制器
 * </p>
 *
 * @author zzp
 * @since 2021-10-09
 */
@RefreshScope // 支持Nacos的动态刷新功能
@RestController
@RequestMapping("/admin/system/post")
@Api(tags = "岗位管理")
public class PostController {

    @Autowired
    private PostService postService;

    @PostMapping("/add")
    @ApiOperation(value = "增加岗位信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.POST_MODUL, operType = OperTypeEnum.ADD, operDesc = "新增岗位信息")
    public CommonResult addPost(@RequestBody @Validated PostForm postForm) {
        postService.addPost(postForm);
        return CommonResult.success("岗位新增成功");
    }



    @PutMapping("/update")
    @ApiOperation(value = "修改岗位信息", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.POST_MODUL, operType = OperTypeEnum.MOD, operDesc = "修改岗位信息")
    public CommonResult updatePost(@RequestBody @Validated PostForm postForm) {
        postService.updatePost(postForm);
        return CommonResult.success("岗位修改成功");
    }

    @PostMapping("/list-page")
    @ApiOperation(value = "查询分页岗位信息", notes = "返回JSONObject")
    public CommonResult<IPage<PostVO>> getPagePost(@RequestBody PostParam postParam) {
        return CommonResult.success(postService.getPagePost(postParam), "查询岗位分页信息成功");
    }

    @GetMapping("/{postId}")
    @ApiOperation(value = "查询岗位信息", notes = "返回JSONObject")
    @ApiImplicitParam(name = "postId", value = "岗位id")
    public CommonResult<PostVO> getOnePost(@PathVariable String postId) {
        return CommonResult.success(postService.getOnePost(postId), "岗位信息查询成功");
    }

    @PutMapping("/forbid")
    @ApiOperation(value = "禁用岗位", notes = "返回JSONObject")
    @OperLog(operModul = ModulEnum.POST_MODUL, operType = OperTypeEnum.MOD, operDesc = "禁用岗位")
    @ApiImplicitParam(name = "postId", value = "岗位id", dataType = "string")
    public CommonResult stopPost(String postId) {
        //构造参数
        LambdaUpdateWrapper<Post> lambdaUpdateWrapper = Wrappers.<Post>lambdaUpdate()
                .eq(Post::getPostId, postId)
                .set(Post::getIsForbid, 0);
        postService.update(lambdaUpdateWrapper);
        return CommonResult.success("岗位禁用成功");
    }

}
